// pages/group/creategroup/creategroup.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.imgurl,
    sendout: 0, //验证码发送状态
    sendCounts: 0,
    updateList: {
      card: {
        showVideo: false,
        showImg: false,
      },
      idcard: {
        showImg: false,
      },
    },
    grouptypecode: 1, // 默认公司类型
    groupinfo:{
      grouptype:'公司单位'  // 默认公司类型
    }
  },
  // 上传
  update: function (e) {
    var that = this
    var o = e.currentTarget.dataset;
    if (o.uptype == 'card'){
      wx.chooseImage({
        sourceType: ['album', 'camera'],
        compressed: 'true',
        maxDuration: 10,
        success: function (res) {
            var updateList = that.data.updateList
            wx.uploadFile({
              url: app.globalData.url + 'Organize/upload',
              filePath: res.tempFilePaths[0],
              name: 'image',
              success: function (res) {
                if (!res.data.code) {
                  var data = JSON.parse(res.data)
                  if (data.code == 1) {
                    updateList[o.uptype].showImg = true
                    updateList[o.uptype].imgUrl = data.path
                    that.setData({
                      updateList: updateList
                    })
                  } else {

                  }
                }


              }
            })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else{
      wx.chooseVideo({
        sourceType: ['album', 'camera'],
        compressed: 'true',
        maxDuration: 10,
        success: function (res) {
          console.log(res)
          var updateList = that.data.updateList
          wx.uploadFile({
            url: app.globalData.url + 'Organize/uploadvideo',
            filePath: res.tempFilePath,
            name: 'video',
            success: function (res) {
              if (!res.data.code) {
                var data = JSON.parse(res.data)
                if (data.code == 1) {
                  updateList[o.uptype].showVideo = true
                  updateList[o.uptype].videoUrl = data.path
                  that.setData({
                    updateList: updateList
                  })
                } else {

                }
              }


            }
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
   
  },
  // 上传组织logo
  updatelogo:function(){
    var that = this
    var groupinfo = this.data.groupinfo
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      compressed: 'true',
      maxDuration: 10,
      success: function (res) {
        console.log(res)
        wx.uploadFile({
          url: app.globalData.url + 'Organize/upload',
          filePath: res.tempFilePaths[0],
          name: 'image',
          success: function (res) {
            if (!res.data.code) {
              var data = JSON.parse(res.data)
              if (data.code == 1) {
                  groupinfo.logoUrl = data.path
                  that.setData({
                    groupinfo: groupinfo
                  })
              
              } else {

              }
            }


          }
        })
       
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 选择组织类型
  groupType: function (e) {
    var o = e.currentTarget.dataset
    var groupinfo = this.data.groupinfo
    groupinfo.grouptype = o.grouptype
    var grouptypecode = o.index
    this.setData({
      grouptypecode: grouptypecode,
      groupinfo: groupinfo
    })
  },
  // 获取姓名
  getName: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.name = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取姓名
  getFullName: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.fullname = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取证照代码
  getCertifycode: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.certifycode = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取微信公众号、官网链接
  getWX: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.wx = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取组织简介
  getdes: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.groupdes = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取联系人
  getlinkman: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.linkman = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取手机号
  getMobile: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.mobile = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 发送验证码
  send: function (e) {
    var that = this
    var timer
    var sendout = 1
    var sendCounts = 10
    var phonenum = this.data.groupinfo.mobile
    
    // 验证手机号 为true ,才发送验证码
    if (app.testPhone(phonenum)) {
      that.setData({
        sendout: sendout,
      })
      wx.request({
        url: app.globalData.url + 'Organize/sendsms?phone=' + phonenum,
        success: function (res) {
          console.log(res)
          that.setData({
            sendCounts:sendCounts
          })
          timer = setInterval(function () {
            sendCounts--;
            if (sendCounts == 0) {
              clearInterval(timer)
              sendout = 0
            }
            that.setData({
              sendCounts: sendCounts,
              sendout: sendout,
            })
          }, 1000)
        }
      })
      
    }

  },
  // 获取验证码
  getQrcode: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.qrcode = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取邮箱
  getEmail: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.email = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  // 获取座机电话
  getPhone: function (e) {
    var groupinfo = this.data.groupinfo
    groupinfo.phone = e.detail.value
    this.setData({
      groupinfo: groupinfo
    })
  },
  summit: function () {
    var groupinfo = this.data.groupinfo
    var updateList = this.data.updateList
    console.log(updateList)
    if (!updateList.card.imgUrl || updateList.card.imgUrl == '') {
      this.showmodal('请上传组织封面')
    } else if (!updateList.idcard.videoUrl || updateList.idcard.videoUrl == '') {
      this.showmodal('请上传身份证')
    } else if (!groupinfo.logoUrl || groupinfo.logoUrl == '') {
      this.showmodal('请上传组织标志')
    } else if (!groupinfo.name || groupinfo.name == '') {
      this.showmodal('组织简称不能为空')
    } else if (!groupinfo.fullname || groupinfo.fullname == '') {
      this.showmodal('组织全称不能为空')
    } else if (!groupinfo.certifycode || groupinfo.certifycode == '') {
      this.showmodal('组织证照不能为空')
    } else if (!groupinfo.groupdes || groupinfo.groupdes == '') {
      this.showmodal('组织简介不能为空')
    } else if (!groupinfo.linkman || groupinfo.linkman == '') {
      this.showmodal('联系人不能为空')
    // } else if (!groupinfo.qrcode || groupinfo.qrcode == '') {
    //   this.showmodal('验证码不能为空')
    } else {
      
      wx.request({
        url:app.globalData.url+ 'Organize/addorganize',
        data: {
          wxopenid:wx.getStorageSync('openId'),
          license: updateList.idcard.imgUrl,
          license_pic: updateList.card.imgUrl,
          sort_title: groupinfo.name,
          title: groupinfo.fullname,
          license_code: groupinfo.certifycode,
          organize_type: groupinfo.grouptype,
          weixing: groupinfo.wx,
          wx_logo: groupinfo.logoUrl,
          description: groupinfo.groupdes,
          organize_name: groupinfo.linkman,
          organize_phone: groupinfo.mobile, // 手机号
          organize_tel: groupinfo.phone,  // 座机号
          organize_email: groupinfo.email,
          code: groupinfo.qrcode
        },
        success: function (res) {
          console.log(res)
          if(res.data.code == 0){
            wx.navigateBack({
              delta: 1,
            })
          }
         },
        fail: function (res) { 
          console.log('fail')
        },
        complete: function (res) { },
      })
    }
  },
  showmodal: function (es) {
    wx.showModal({
      title: '提示',
      content: es,
      confirmColor: '#ff8e03',
      showCancel: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})